package com.ruoyi.project.system.course.controller;

import java.util.List;

import com.ruoyi.project.monitor.server.domain.Sys;
import com.ruoyi.project.system.course.domain.*;
import com.ruoyi.project.system.course.service.*;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 课程 信息操作处理
 * 
 * @author Waynnn
 * @date 2019-03-21
 */
@Controller
@RequestMapping(value = "system/course")
public class CourseController extends BaseController
{
    private String prefix = "system/course";
	
	@Autowired
	private ICourseService courseService;

	@Autowired
	private IUserService userService;

	@Autowired
	private IDeptService deptService;
	
	@RequiresPermissions("system:course:view")
	@GetMapping()
	public String course()
	{
	    return prefix + "/course";
	}
	
	/**
	 * 查询课程列表
	 */
	@RequiresPermissions("system:course:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Course course)
	{

		startPage();
        List<Course> list = courseService.selectCourseList(course);
		return getDataTable(list);
	}
	

	
	/**
	 * 新增课程
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{

		mmap.put("teachers", userService.selectTeacherList());
		mmap.put("classes", deptService.selectClass());
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存课程
	 */
	@RequiresPermissions("system:course:add")
	@Log(title = "课程", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Course course)
	{

		return toAjax(courseService.insertCourse(course));
	}

	/**
	 * 修改课程
	 */
	@GetMapping("/edit/{courseId}")
	public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap)
	{

		mmap.put("course", courseService.selectCourseById(courseId));
		mmap.put("teachers",userService.selectTeacherByCourseId(courseId));
		mmap.put("classes",deptService.selectClassByCourseId(courseId));

	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存课程
	 */
	@RequiresPermissions("system:course:edit")
	@Log(title = "课程", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Course course)
	{		
		return toAjax(courseService.updateCourse(course));
	}
	
	/**
	 * 删除课程
	 */
	@RequiresPermissions("system:course:remove")
	@Log(title = "课程", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		try {
			return toAjax(courseService.deleteCourseByIds(ids));
		} catch (Exception e) {
			return error(e.getMessage());
		}
	}

	@Log(title = "课程", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:course:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(Course course)
	{
		return toAjax(courseService.changeStatus(course));
	}
}
